<?php

class HelpController extends BaseController
{

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		$this->performAjaxValidation($model);

		if(isset($_POST['Help']))
		{
			$model->attributes=$_POST['Help'];
			$model->attributes=$_POST['Help'];
			if($_POST['cateid3']>0){
				$model->zhc_id = (int)$_POST['cateid3'];
			}elseif($_POST['cateid2']>0){
				$model->zhc_id = (int)$_POST['cateid2'];
			}elseif($_POST['cateid1']>0){
				$model->zhc_id = (int)$_POST['cateid1'];
			}else{
				//失败跳转
				$this->render('../index/error',array('message'=>'请选择类目！'));
				exit;
			}
			if($model->save()){
				//成功跳转
				$this->render('../index/success',array(
						'message'=>'恭喜，帮助信息发布成功',
						'links'=>array(
								array('继续发布帮助信息',$this->pre_module_url.'/helpCreate/create'),
								array('帮助信息管理',$this->pre_module_url.'/help/admin'),
								array('前台浏览帮助信息','/home/help/detail/id/'.$model->zhe_id),
						),
				));
				exit;
			}
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete()
	{
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zhe_id in ($id)");
		
		//执行操作  
		if(Help::model()->deleteAll($criteria)){  
		    //成功跳转  
		    $this->redirect(Yii::app()->request->urlReferrer);
		}  
	}


	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Help('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Help'])){
			$model->attributes=$_GET['Help'];
			if($_GET['cateid1']>0)
				$model->zhc_id = (int)$_GET['cateid1'];
			if($_GET['cateid2']>0)
				$model->zhc_id = (int)$_GET['cateid2'];
		}

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Help::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='help-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
